package com.atguigu.gmall.publisher.controller;

import com.atguigu.gmall.publisher.bean.TrafficVisitorStatsPerHour;
import com.atguigu.gmall.publisher.bean.UserAction;
import com.atguigu.gmall.publisher.service.UserActionServince;
import com.atguigu.gmall.publisher.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserActionController {
    @Autowired
    private UserActionServince userActionServince;
    @RequestMapping("/userAction")
    public String getVisitorPerHr(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<UserAction> userAction = userActionServince.getUserAction(date);
        if (userAction == null || userAction.size() == 0) {
            return "无数据";
        }

        Long huc = 0L;
        Long gduc = 0L;
        Long ouc = 0L;
        Long cauc = 0L;
        Long pmsu = 0L;
        for (int i = 0; i < userAction.size(); i++) {
             huc = userAction.get(i).getHuc();
            gduc = userAction.get(i).getGduc();
             ouc = userAction.get(i).getOuc();
             cauc = userAction.get(i).getCauc();
            pmsu = userAction.get(i).getPmsu();
        }
        String json = "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"name\": \"首页浏览人数\",\n" +
                "      \"value\":"+ huc+",\n" +
                "      \"url\": \"http://aipage.baidu.com\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"商品详情页浏览人数\",\n" +
                "      \"value\": "+gduc+",\n" +
                "      \"url\": \"http://aipage.baidu.com\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"当日加购人数\",\n" +
                "      \"value\": "+cauc+",\n" +
                "      \"url\": \"http://aipage.baidu.com\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"当日下单人数\",\n" +
                "      \"value\": "+ouc+",\n" +
                "      \"url\": \"http://aipage.baidu.com\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"当日支付成功人数\",\n" +
                "      \"value\": "+pmsu+",\n" +
                "      \"url\": \"http://aipage.baidu.com\"\n" +
                "    }\n" +
                "  ]\n" +
                "}";
        return json;
    }
}
